<?php

class PagesView
{
    public function DoNewPageForm( $msg )
    {
        $html = '';

        $html .= '<form  action="" method="post">';
        $html .= '    <fieldset>';
        $html .= '        <label for="page_title">Title:</label> <input type="text" name="page_title" id="page_title" />';
        $html .= '        <label for="page_published">Published:</label> <input type="checkbox" name="page_published" id="page_published" />';
        $html .= '        <input type="submit" name="page_add" value="Add Page" />';
        $html .= '    </fieldset>';
        $html .= '<form>';
        
        if ( isset( $msg['add']['msg'] ) )
        {
            foreach ( $msg['add']['msg'] as $msg )
            {
                $html .= '<p>' . $msg .  '</p>';
            }
        }
        
        if ( isset( $msg['add']['error'] ) )
        {
            foreach ( $msg['add']['error'] as $error )
            {
                $html .= '<p>' . $error .  '</p>';
            }
        }
        
        return $html;
    }
    
    public function DoListPages( $pages, $msg )
    {
        $html = '';
		
		$html .= '<table>' . "\r\n";
        $html .= '    <thead>' . "\r\n";
        $html .= '        <tr>' . "\r\n";
        $html .= '            <th>PageID</th>' . "\r\n";
        $html .= '            <th>Title</th>' . "\r\n";
        $html .= '            <th>Published</th>' . "\r\n";
        $html .= '        </tr>' . "\r\n";
        $html .= '    </thead>' . "\r\n";
        
        $html .= '    <tbody>' . "\r\n";
		
		$pageCount = count( $pages );
        for( $i = 0; $i < $pageCount; $i++ )
        {
            $html .= '        <tr>' . "\r\n";
            $html .= '            <td>' . $pages[$i]['id'] . '</td>' . "\r\n";
            $html .= '            <td>' . $pages[$i]['title'] . '</td>' . "\r\n";
            $html .= '            <td>' . $pages[$i]['published'] . '</td>' . "\r\n";
            $html .= '            <td>' . "\r\n";
            $html .= '                <form action="" method="POST">' . "\r\n";
            $html .= '                    <fieldset>' . "\r\n";
            $html .= '                        <input type="hidden" name="list_pageID" value="' . $pages[$i]['id'] . '" />' . "\r\n";
            $html .= '                        <input type="hidden" name="list_title" value="' . $pages[$i]['title'] . '" />' . "\r\n";
            $html .= '                        <input type="submit" name="list_editOption" value="Edit" />' . "\r\n";
            $html .= '                        <input type="submit" name="list_editOption" value="Delete" />' . "\r\n";
            $html .= '                    </fieldset>' . "\r\n";
            $html .= '                </form>' . "\r\n";
            $html .= '            </td>' . "\r\n";
            $html .= '        </tr>' . "\r\n";
        }
        $html .= '    </tbody>' . "\r\n";
        
        $html .= '</table>' . "\r\n";
		
        
        if ( isset( $msg['delete']['msg'] ) )
        {
            foreach ( $msg['delete']['msg'] as $msg )
            {
                $html .= '<p>' . $msg .  '</p>';
            }
        }
        
        if ( isset( $msg['delete']['error'] ) )
        {
            foreach ( $msg['delete']['error'] as $error )
            {
                $html .= '<p>' . $error .  '</p>';
            }
        }
		
		if ( isset( $msg['edit']['msg'] ) )
        {
            foreach ( $msg['edit']['msg'] as $msg )
            {
                $html .= '<p>' . $msg .  '</p>';
            }
        }
        
        if ( isset( $msg['edit']['error'] ) )
        {
            foreach ( $msg['edit']['error'] as $error )
            {
                $html .= '<p>' . $error .  '</p>';
            }
        }
        
        return $html;
    }
	
	public function DoEditform( $page )
	{
		$html = '';
		
		$html = '';
        
		$html .= '<form action="" method="POST">' . "\n\r";
        $html .= '    <fieldset>' . "\n\r";
		$html .= '        <input type="hidden" name="edit_pageID" value="' . $page['id'] . '" />' . "\r\n";
        
		$html .= '        <label for="edit_username">Title:</label>' . "\n\r";
		$html .= '        <input type="text" name="edit_title" id="edit_title" value="' . $page['title'] . '" />' . "\r\n";

		$html .= '        <label for="edit_email">Published:</label>' . "\r\n";
		$html .= '        <input type="text" name="edit_published" id="edit_published" value="' . $page['published'] . '" />' . "\r\n";
		$html .= '        <input type="submit" name="edit_save" value="Save" /> <input type="submit" name="edit_save" value="Cancel" />' . "\r\n";
        $html .= '    </fieldset>' . "\n\r";
		$html .= '</form>' . "\r\n";
		
		return $html;
		
		return $html;
	}
	
    
    public function TriedToAddPage()
    {
        return isset( $_POST['page_add'] );
    }
    
    public function GetAddPageData()
    {
		$published = isset( $_POST['page_published'] ) ? 1 : 0;
        return array( "title" => $_POST['page_title'],
                      "published" => $published );
    }
	
	public function GetEditPageData()
    {   
		return array( 	"id" => $_POST['edit_pageID'],
						"title" => $_POST['edit_title'],
                      	"published" => $_POST['edit_published'] );
    }

	/**
	 * Checks if the POST DeletePage is set.
	 * @return bool True/False depending on if the user clicked "Delete page" in the page list view.
	 */
	public function DeletePage()
	{
        return isset( $_POST['list_editOption'] ) && $_POST['list_editOption'] === "Delete";
	}
	
	/**
	 * Checks if the POST EditPage is set.
	 * @return bool True/False depending on if the user clicked "Edit Page" in the user list view.
	 */
	public function EditPage()
	{
        return isset( $_POST['list_editOption'] ) && $_POST['list_editOption'] === "Edit";
	}
	
	/**
	 * Checks if the user clicked on Save.
	 * @return bool True/False depending on if the user clicked "Save" in the user edit view.
	 */
    public function EditSave()
    {
        return isset( $_POST['edit_save'] ) && $_POST['edit_save'] === "Save";
    }
	
	/**
	 * Checks if the user clicked on Cancel.
	 * @return bool True/False depending on if the user clicked "Cancel" in the user edit view.
	 */
    public function EditCancel()
    {
        return isset( $_POST['edit_save'] ) && $_POST['edit_save'] === "Cancel";
    }

    /**
	 * Gets the PageID from POST.
	 * @return int Returns the pageID for the Page, from the Page list, to edit.
	 */
    public function GetListPageID()
    {
        return ( isset( $_POST['list_pageID'] ) ) ? $_POST['list_pageID'] : NULL;
    }
    
    /**
	 * Gets the title from POST.
	 * @return string Returns the title for the page, from the page list, to edit.
	 */
    public function GetListTitle()
    {
        return ( isset( $_POST['list_title'] ) ) ? $_POST['list_title'] : NULL;
    }
	
	public function GetListPublished()
	{
		return ( isset( $_POST['list_published'] ) ) ? $_POST['list_published'] : NULL;
	}
	
}
